{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a4d5d1bc-df65-47ae-8e2a-d1fe6218fb3e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T11:08:31.225736Z",
     "iopub.status.busy": "2022-06-01T11:08:31.225042Z",
     "iopub.status.idle": "2022-06-01T11:08:31.230702Z",
     "shell.execute_reply": "2022-06-01T11:08:31.230045Z",
     "shell.execute_reply.started": "2022-06-01T11:08:31.225706Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 33 µs, sys: 1 µs, total: 34 µs\n",
      "Wall time: 37.4 µs\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "from torch.utils.data import Dataset\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import os\n",
    "\n",
    "import torchvision.transforms as transforms\n",
    "from torchvision.utils import save_image\n",
    "from torch.utils.data import DataLoader\n",
    "import torch.utils.data as Data\n",
    "from torchvision import datasets\n",
    "import torch.autograd as autograd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "029bc639-dace-4661-875b-ec0605f5f8e2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T11:08:31.231935Z",
     "iopub.status.busy": "2022-06-01T11:08:31.231754Z",
     "iopub.status.idle": "2022-06-01T11:08:31.238643Z",
     "shell.execute_reply": "2022-06-01T11:08:31.238090Z",
     "shell.execute_reply.started": "2022-06-01T11:08:31.231915Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GPU:False\n"
     ]
    }
   ],
   "source": [
    "class Parse:\n",
    "    def __init__(self):  #定义超参数\n",
    "        self.channels = 1\n",
    "        self.img_size = 28\n",
    "        self.latent_dim = 100\n",
    "        self.epochs = 200\n",
    "        self.batch_size = 64\n",
    "        self.lr = 0.00005\n",
    "        self.cpu_nums = 8\n",
    "        self.classes = 10\n",
    "        self.want_cuda = \"cuda:0\"\n",
    "        self.clip_value = 0.01\n",
    "        self.n_critic = 5  # 训练5次D，训练1次G\n",
    "        self.b = (0.5, 0.999)\n",
    "        \n",
    "        self.lambda_gp = 10  # 不知道干啥的\n",
    "        \n",
    "        \n",
    "\n",
    "        # 下面是衍生的\n",
    "        self.img_shape = (self.channels, self.img_size, self.img_size)\n",
    "        self.cuda = torch.cuda.is_available()\n",
    "        self.device = self.want_cuda if self.cuda else 'cpu'\n",
    "        self.big_cuda = True  # 能不能把数据集全部放入GPU\n",
    "        print(f\"GPU:{self.cuda}\")\n",
    "opt = Parse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9a9197d7-5606-4b26-804b-c4e64a5f1354",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T11:08:31.239877Z",
     "iopub.status.busy": "2022-06-01T11:08:31.239508Z",
     "iopub.status.idle": "2022-06-01T11:08:31.247265Z",
     "shell.execute_reply": "2022-06-01T11:08:31.246700Z",
     "shell.execute_reply.started": "2022-06-01T11:08:31.239856Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "class Tools:\n",
    "    # 装一些杂七杂八的工具\n",
    "    def block(in_feat, out_feat, normalize=True):\n",
    "        layers = [nn.Linear(in_feat, out_feat),nn.LeakyReLU(0.2, inplace=True)]\n",
    "        if normalize:\n",
    "            layers.insert(1, nn.BatchNorm1d(out_feat, 0.8))\n",
    "        return layers\n",
    "    \n",
    "    \n",
    "    def sample_image(n_row, batches_done, generator):\n",
    "\n",
    "        z = torch.randn(n_row ** 2, opt.latent_dim, device=opt.device)\n",
    "        gen_imgs = generator(z)\n",
    "        save_image(gen_imgs.data, \"images/%d.png\" % batches_done, nrow=n_row, normalize=True)\n",
    "        \n",
    "    def gradient_penalty(D, real_samples, fake_samples):\n",
    "    \n",
    "        alpha = torch.randn(real_samples.size(0), 1, 1, 1, requires_grad=True)\n",
    "        interpolates = (alpha * real_samples + ((1 - alpha) * fake_samples))#.requires_grad_(True)\n",
    "        d_interpolates = D(interpolates)\n",
    "        \n",
    "        fake = torch.ones(real_samples.shape[0], 1)\n",
    "\n",
    "        gradients = autograd.grad(\n",
    "            outputs=d_interpolates,\n",
    "            inputs=interpolates,\n",
    "            grad_outputs=fake,\n",
    "            create_graph=True,\n",
    "            retain_graph=True,\n",
    "            only_inputs=True,\n",
    "        )[0]\n",
    "        gradients = gradients.view(gradients.size(0), -1)\n",
    "        gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean()\n",
    "        return gradient_penalty\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c8d46030-6bfa-40f4-a98e-9208c741d7b9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T11:08:31.248690Z",
     "iopub.status.busy": "2022-06-01T11:08:31.248451Z",
     "iopub.status.idle": "2022-06-01T11:08:31.254264Z",
     "shell.execute_reply": "2022-06-01T11:08:31.253739Z",
     "shell.execute_reply.started": "2022-06-01T11:08:31.248670Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "class Generator(nn.Module):\n",
    "    def __init__(self):\n",
    "        \n",
    "        super().__init__()\n",
    "\n",
    "        \n",
    "        self.model = nn.Sequential(\n",
    "            *Tools.block(opt.latent_dim, 128, normalize=False),\n",
    "            *Tools.block(128, 256),\n",
    "            *Tools.block(256, 512),\n",
    "            *Tools.block(512, 1024),\n",
    "            nn.Linear(1024, int(np.prod(opt.img_shape))),\n",
    "            nn.Tanh())\n",
    "        \n",
    "        self.optimizer = torch.optim.Adam(self.parameters(), lr=opt.lr, betas=opt.b)\n",
    "        \n",
    "    def forward(self, noise):\n",
    "        img = self.model(noise)\n",
    "        img = img.view(img.size(0), *opt.img_shape)\n",
    "        return img\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9724604b-1ac2-4ceb-a049-bb826b26aec3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T11:08:31.255207Z",
     "iopub.status.busy": "2022-06-01T11:08:31.255044Z",
     "iopub.status.idle": "2022-06-01T11:08:31.260946Z",
     "shell.execute_reply": "2022-06-01T11:08:31.260293Z",
     "shell.execute_reply.started": "2022-06-01T11:08:31.255188Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "class Discriminator(nn.Module):\n",
    "    def __init__(self):\n",
    "        \n",
    "        super().__init__()\n",
    "        \n",
    "\n",
    "        self.model = nn.Sequential(\n",
    "            nn.Linear(int(np.prod(opt.img_shape)), 512),\n",
    "            nn.LeakyReLU(0.2, inplace=True),\n",
    "            nn.Linear(512, 256),\n",
    "            nn.LeakyReLU(0.2, inplace=True),\n",
    "            nn.Linear(256, 1),\n",
    "        )\n",
    "        self.optimizer = torch.optim.Adam(self.parameters(), lr=opt.lr, betas=opt.b)\n",
    "        \n",
    "\n",
    "    def forward(self, imgs):\n",
    "        a = imgs.view(imgs.shape[0], -1)\n",
    "        validity = self.model(a)\n",
    "        return validity\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d3f43013-2776-4506-8ada-ea7b2674dc64",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T11:08:31.262130Z",
     "iopub.status.busy": "2022-06-01T11:08:31.261941Z",
     "iopub.status.idle": "2022-06-01T11:08:31.275861Z",
     "shell.execute_reply": "2022-06-01T11:08:31.275208Z",
     "shell.execute_reply.started": "2022-06-01T11:08:31.262105Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "class Dev:\n",
    "    def __init__(self):\n",
    "        self.g = Generator()\n",
    "        self.d = Discriminator()\n",
    "        # self.loss = nn.MSELoss()\n",
    "        \n",
    "        self.models = (self.g, self.d)\n",
    "        \n",
    "        self.losses1 = []\n",
    "        self.losses2 = []\n",
    "        \n",
    "    def load_data(self):\n",
    "\n",
    "        def pretreat():\n",
    "            train = pd.read_csv('../../data/mnist/train.csv', header=None).values\n",
    "            # train, train_label = torch.tensor(train[...,1:]/255, dtype=torch.float), torch.tensor(train[...,0], dtype=torch.long)\n",
    "            train, train_label = torch.tensor((train[...,1:]/255-0.5)/0.5, dtype=torch.float).reshape(-1, 1, 28, 28), torch.tensor(train[...,0], dtype=torch.long)\n",
    "            return [train, train_label]\n",
    "        self.data = pretreat()\n",
    "        self.dataloader = torch.utils.data.DataLoader(\n",
    "            Data.TensorDataset(*self.data),\n",
    "            batch_size=opt.batch_size,\n",
    "            shuffle=True,\n",
    "            num_workers=opt.cpu_nums,\n",
    "            drop_last=True)\n",
    "        \n",
    "    def to_cuda(self):\n",
    "        opt.cuda = True\n",
    "        opt.device = opt.want_cuda\n",
    "        for i in self.models:\n",
    "            i.cuda()\n",
    "        if opt.big_cuda:\n",
    "            self.dataloader = torch.utils.data.DataLoader(\n",
    "                Data.TensorDataset(*[i.to(opt.device) for i in self.data]),\n",
    "                batch_size=opt.batch_size,\n",
    "                shuffle=True,\n",
    "                # num_workers=opt.cpu_nums,\n",
    "                drop_last=True)\n",
    "       \n",
    "    def to_cpu(self):\n",
    "        opt.cuda = False\n",
    "        opt.device = 'cpu'\n",
    "        for i in self.models:\n",
    "            i.cpu()\n",
    "        self.dataloader = torch.utils.data.DataLoader(\n",
    "            Data.TensorDataset(*self.data),\n",
    "            batch_size=opt.batch_size,\n",
    "            shuffle=True,\n",
    "            num_workers=opt.cpu_nums,\n",
    "            drop_last=True)\n",
    "        \n",
    "        \n",
    "        \n",
    "    def train(self):\n",
    "        \n",
    "        losses1 = []\n",
    "        \n",
    "        for i, (imgs, labels) in enumerate(self.dataloader):\n",
    "            \n",
    "            z = torch.randn(imgs.shape[0], opt.latent_dim, device=opt.device)\n",
    "            fake_imgs = self.g(z)\n",
    "            fake_imgs2 = fake_imgs.detach() # 一个给d用，一个给g用\n",
    "        \n",
    "            if i % opt.n_critic == 0:\n",
    "                self.g.optimizer.zero_grad()\n",
    "                loss_G = - torch.mean(self.d(fake_imgs))\n",
    "                loss_G.backward()\n",
    "                self.g.optimizer.step()\n",
    "            \n",
    "            self.d.optimizer.zero_grad()\n",
    "            d_fake_loss = - torch.mean(self.d(imgs)) + torch.mean(self.d(fake_imgs2)) + opt.lambda_gp * Tools.gradient_penalty(self.d, fake_imgs2, imgs)\n",
    "            d_fake_loss.backward()\n",
    "            self.d.optimizer.step()\n",
    "            \n",
    "            #for p in self.d.parameters():  # gp去掉\n",
    "                #p.data.clamp_(-opt.clip_value, opt.clip_value)\n",
    "\n",
    "            losses1.append(d_fake_loss.item())\n",
    "        self.losses1.append(sum(losses1)/len(losses1))\n",
    "        print(self.losses1[-1], end=' ')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "550e8797-867c-4db5-8628-9ca63d19d811",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T11:09:37.040714Z",
     "iopub.status.busy": "2022-06-01T11:09:37.040204Z",
     "iopub.status.idle": "2022-06-01T11:09:40.320002Z",
     "shell.execute_reply": "2022-06-01T11:09:40.319428Z",
     "shell.execute_reply.started": "2022-06-01T11:09:37.040684Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "dev = Dev()\n",
    "dev.load_data()\n",
    "# dev.to_cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c6a63a06-bc71-4791-99b6-d888511378f3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-01T11:09:40.321272Z",
     "iopub.status.busy": "2022-06-01T11:09:40.321082Z",
     "iopub.status.idle": "2022-06-01T11:59:01.256481Z",
     "shell.execute_reply": "2022-06-01T11:59:01.255848Z",
     "shell.execute_reply.started": "2022-06-01T11:09:40.321252Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 1 -17.460722316163835 耗时14.655099391937256s\n",
      "epoch 2 -1.9069561781471032 耗时14.883773803710938s\n",
      "epoch 3 -5.365885394868881 耗时14.633859395980835s\n",
      "epoch 4 -5.196386935998434 耗时14.760331392288208s\n",
      "epoch 5 -5.064407962109007 耗时14.862134218215942s\n",
      "epoch 6 -4.884474764511451 耗时14.956905841827393s\n",
      "epoch 7 -5.017283349052438 耗时14.77856969833374s\n",
      "epoch 8 -5.35620064872815 耗时14.782985925674438s\n",
      "epoch 9 -5.468643682107345 耗时14.79577922821045s\n",
      "epoch 10 -5.477136165126283 耗时14.790254354476929s\n",
      "epoch 11 -5.2957213217573464 耗时14.72071647644043s\n",
      "epoch 12 -4.889590504584215 耗时14.74716067314148s\n",
      "epoch 13 -5.087082512732376 耗时14.644119262695312s\n",
      "epoch 14 -5.256808005122263 耗时14.820020914077759s\n",
      "epoch 15 -5.0853814045673 耗时15.067323207855225s\n",
      "epoch 16 -4.971724372790488 耗时14.949085712432861s\n",
      "epoch 17 -5.122899460563538 耗时14.798457384109497s\n",
      "epoch 18 -5.017370817373759 耗时14.70806884765625s\n",
      "epoch 19 -5.199444511784051 耗时14.693356275558472s\n",
      "epoch 20 -5.3490812007492865 耗时14.827829122543335s\n",
      "epoch 21 -5.2413552680163145 耗时14.908379554748535s\n",
      "epoch 22 -5.3058441242515215 耗时14.945027589797974s\n",
      "epoch 23 -4.865064890687468 耗时14.67587399482727s\n",
      "epoch 24 -4.985667833778113 耗时14.781451225280762s\n",
      "epoch 25 -4.887533533407759 耗时14.611241817474365s\n",
      "epoch 26 -4.783860828731459 耗时14.759914875030518s\n",
      "epoch 27 -4.595098158338663 耗时14.886985301971436s\n",
      "epoch 28 -4.619684965658849 耗时14.662856578826904s\n",
      "epoch 29 -4.615967733501243 耗时14.577140092849731s\n",
      "epoch 30 -4.5640441419729685 耗时14.808647871017456s\n",
      "epoch 31 -4.368645489661136 耗时14.698498725891113s\n",
      "epoch 32 -4.405919918255719 耗时14.725627422332764s\n",
      "epoch 33 -4.31199998585875 耗时14.931107759475708s\n",
      "epoch 34 -4.156523865086037 耗时14.775043487548828s\n",
      "epoch 35 -4.038037872110894 耗时14.950200080871582s\n",
      "epoch 36 -4.009288111771183 耗时14.658700466156006s\n",
      "epoch 37 -3.91933516046408 耗时14.941789388656616s\n",
      "epoch 38 -3.895497988992974 耗时14.605137586593628s\n",
      "epoch 39 -3.8319255402401136 耗时14.731062173843384s\n",
      "epoch 40 -3.800730392543585 耗时14.871744871139526s\n",
      "epoch 41 -3.7807975540547893 耗时14.95755672454834s\n",
      "epoch 42 -3.7753976783223187 耗时14.89712119102478s\n",
      "epoch 43 -3.7221117739743588 耗时14.895877838134766s\n",
      "epoch 44 -3.6294504018577975 耗时14.836143732070923s\n",
      "epoch 45 -3.6313136977599805 耗时14.782770156860352s\n",
      "epoch 46 -3.6567772936032195 耗时14.772922992706299s\n",
      "epoch 47 -3.521004549848868 耗时14.63348650932312s\n",
      "epoch 48 -3.646659514438381 耗时14.698827981948853s\n",
      "epoch 49 -3.4960910103491556 耗时14.605395793914795s\n",
      "epoch 50 -3.454246177235688 耗时14.772197246551514s\n",
      "epoch 51 -3.438009228752161 耗时14.732722520828247s\n",
      "epoch 52 -3.430573553769286 耗时14.87991189956665s\n",
      "epoch 53 -3.390596977547368 耗时14.667059898376465s\n",
      "epoch 54 -3.3055727754102318 耗时14.717179775238037s\n",
      "epoch 55 -3.399628963897933 耗时14.729796886444092s\n",
      "epoch 56 -3.24889257979622 耗时14.79963731765747s\n",
      "epoch 57 -3.263785088456682 耗时14.544796466827393s\n",
      "epoch 58 -3.1665515006542715 耗时14.627588272094727s\n",
      "epoch 59 -3.2058805373587758 耗时14.577150821685791s\n",
      "epoch 60 -3.0984429567575202 耗时14.864330053329468s\n",
      "epoch 61 -3.092966161644446 耗时14.515881776809692s\n",
      "epoch 62 -3.0494926828203233 耗时14.737488508224487s\n",
      "epoch 63 -3.0036029772417523 耗时14.806015014648438s\n",
      "epoch 64 -2.9473012806638965 耗时15.046717166900635s\n",
      "epoch 65 -2.9688682467095244 耗时14.841181755065918s\n",
      "epoch 66 -2.942332872077266 耗时14.91354513168335s\n",
      "epoch 67 -2.9546047961826387 耗时14.679327964782715s\n",
      "epoch 68 -2.9424449914038626 耗时14.61577558517456s\n",
      "epoch 69 -2.8441889573568466 耗时14.638838291168213s\n",
      "epoch 70 -2.913407810215253 耗时14.637738704681396s\n",
      "epoch 71 -2.8439917762862197 耗时14.553885221481323s\n",
      "epoch 72 -2.7783700895665677 耗时14.52338433265686s\n",
      "epoch 73 -2.8160324693362258 耗时14.801445007324219s\n",
      "epoch 74 -2.751013298044846 耗时14.62943148612976s\n",
      "epoch 75 -2.726537682330723 耗时14.69247579574585s\n",
      "epoch 76 -2.7361580529106084 耗时14.884965419769287s\n",
      "epoch 77 -2.6936919920726927 耗时14.697218894958496s\n",
      "epoch 78 -2.697040744881238 耗时14.697694063186646s\n",
      "epoch 79 -2.6973767456148323 耗时15.082897424697876s\n",
      "epoch 80 -2.6932460271943213 耗时14.917734622955322s\n",
      "epoch 81 -2.6167468865627668 耗时14.904654026031494s\n",
      "epoch 82 -2.6254659375869602 耗时14.732465267181396s\n",
      "epoch 83 -2.5775064513675687 耗时14.86396598815918s\n",
      "epoch 84 -2.599854749956406 耗时15.006906747817993s\n",
      "epoch 85 -2.540899781305482 耗时14.985211372375488s\n",
      "epoch 86 -2.5326030463012077 耗时14.779608488082886s\n",
      "epoch 87 -2.5028530387257564 耗时14.675662994384766s\n",
      "epoch 88 -2.474121613334566 耗时14.650131940841675s\n",
      "epoch 89 -2.471353371344356 耗时14.734833240509033s\n",
      "epoch 90 -2.438391539177747 耗时14.786008596420288s\n",
      "epoch 91 -2.4193475257879644 耗时14.796179056167603s\n",
      "epoch 92 -2.4120545300755456 耗时14.69747805595398s\n",
      "epoch 93 -2.392403481227993 耗时14.84480619430542s\n",
      "epoch 94 -2.3679709578272883 耗时14.801005363464355s\n",
      "epoch 95 -2.3437709762548815 耗时14.880512475967407s\n",
      "epoch 96 -2.3309932003153553 耗时14.840628385543823s\n",
      "epoch 97 -2.2910399821168457 耗时14.860879182815552s\n",
      "epoch 98 -2.29107020262593 耗时14.82968258857727s\n",
      "epoch 99 -2.2596670810288275 耗时14.765711784362793s\n",
      "epoch 100 -2.2544533839984027 耗时14.658055782318115s\n",
      "epoch 101 -2.2428755159693567 耗时14.763508081436157s\n",
      "epoch 102 -2.2039360154018586 耗时14.641476392745972s\n",
      "epoch 103 -2.200818326200976 耗时14.706574440002441s\n",
      "epoch 104 -2.192277901073403 耗时14.742579221725464s\n",
      "epoch 105 -2.1690321840115265 耗时14.675625562667847s\n",
      "epoch 106 -2.1638394994949457 耗时14.80121111869812s\n",
      "epoch 107 -2.1328983464643057 耗时14.788557052612305s\n",
      "epoch 108 -2.1105806655792185 耗时14.84786319732666s\n",
      "epoch 109 -2.102995181796136 耗时14.799873113632202s\n",
      "epoch 110 -2.101953995265981 耗时14.829870462417603s\n",
      "epoch 111 -2.0872714583112946 耗时14.69026517868042s\n",
      "epoch 112 -2.089106622347837 耗时14.957574367523193s\n",
      "epoch 113 -2.0686456779278455 耗时14.99980354309082s\n",
      "epoch 114 -2.043079559678329 耗时14.850131034851074s\n",
      "epoch 115 -2.0291321291613094 耗时14.872851610183716s\n",
      "epoch 116 -2.026788613330593 耗时14.86753511428833s\n",
      "epoch 117 -2.034617047172473 耗时14.677754878997803s\n",
      "epoch 118 -2.0141495262673215 耗时15.03335452079773s\n",
      "epoch 119 -1.9941442012786865 耗时14.829977989196777s\n",
      "epoch 120 -1.9886055247124639 耗时14.948753356933594s\n",
      "epoch 121 -1.9975967925029987 耗时14.619462013244629s\n",
      "epoch 122 -1.9699202367564785 耗时14.579043865203857s\n",
      "epoch 123 -1.985871049485059 耗时14.83374285697937s\n",
      "epoch 124 -1.9462186591729538 耗时14.913884401321411s\n",
      "epoch 125 -1.956923246383667 耗时15.006544589996338s\n",
      "epoch 126 -1.9278291437134798 耗时14.901119947433472s\n",
      "epoch 127 -1.9247756636638906 耗时14.813130617141724s\n",
      "epoch 128 -1.912083207479536 耗时14.755533933639526s\n",
      "epoch 129 -1.8919339821203796 耗时14.638678550720215s\n",
      "epoch 130 -1.8831857006476045 耗时14.88966703414917s\n",
      "epoch 131 -1.8835375450209595 耗时14.7079176902771s\n",
      "epoch 132 -1.8877796784536307 耗时14.803199291229248s\n",
      "epoch 133 -1.8672636268996379 耗时14.670432090759277s\n",
      "epoch 134 -1.8545682759778603 耗时14.627612352371216s\n",
      "epoch 135 -1.8417678291540645 耗时14.785217046737671s\n",
      "epoch 136 -1.8344078778966133 耗时14.979514837265015s\n",
      "epoch 137 -1.8440739641576338 耗时15.039226293563843s\n",
      "epoch 138 -1.8322225184425345 耗时14.85319995880127s\n",
      "epoch 139 -1.812825187929412 耗时14.970863819122314s\n",
      "epoch 140 -1.7995579042963945 耗时14.836648941040039s\n",
      "epoch 141 -1.8012966146591252 耗时14.632606267929077s\n",
      "epoch 142 -1.7921092874085254 耗时14.558125019073486s\n",
      "epoch 143 -1.7890871265781854 耗时14.719377040863037s\n",
      "epoch 144 -1.7702418115375644 耗时14.65286636352539s\n",
      "epoch 145 -1.7721445001940082 耗时14.886072158813477s\n",
      "epoch 146 -1.761670123169491 耗时14.874848365783691s\n",
      "epoch 147 -1.7471641158345161 耗时14.812771558761597s\n",
      "epoch 148 -1.7508639676084894 耗时14.915638446807861s\n",
      "epoch 149 -1.7509964028951834 耗时14.87800121307373s\n",
      "epoch 150 -1.7546500262735747 耗时14.709028959274292s\n",
      "epoch 151 -1.7325817860559567 耗时14.916003942489624s\n",
      "epoch 152 -1.726943350906942 耗时14.874329566955566s\n",
      "epoch 153 -1.7198320220094354 耗时15.072495222091675s\n",
      "epoch 154 -1.7193871315413631 耗时14.916266918182373s\n",
      "epoch 155 -1.7101412204948025 耗时15.044577836990356s\n",
      "epoch 156 -1.7018052072382532 耗时14.956360816955566s\n",
      "epoch 157 -1.7074217686912676 耗时14.940136194229126s\n",
      "epoch 158 -1.6910433128014954 耗时14.72718596458435s\n",
      "epoch 159 -1.6877041865565607 耗时14.768339395523071s\n",
      "epoch 160 -1.6682372184802272 耗时14.860902309417725s\n",
      "epoch 161 -1.674994276707302 耗时14.785950183868408s\n",
      "epoch 162 -1.67048618696797 耗时14.832990407943726s\n",
      "epoch 163 -1.6639868882320734 耗时14.905616283416748s\n",
      "epoch 164 -1.6590457130521694 耗时14.948028087615967s\n",
      "epoch 165 -1.6628123288984105 耗时15.035624504089355s\n",
      "epoch 166 -1.6565249857424162 耗时14.66762638092041s\n",
      "epoch 167 -1.655204094079668 耗时14.834598779678345s\n",
      "epoch 168 -1.639982656582697 耗时14.867015600204468s\n",
      "epoch 169 -1.6305545158803527 耗时14.818103790283203s\n",
      "epoch 170 -1.6276309837526548 耗时14.860563278198242s\n",
      "epoch 171 -1.6227822899182298 耗时15.107815980911255s\n",
      "epoch 172 -1.6223093334867706 耗时14.74706482887268s\n",
      "epoch 173 -1.6236454422725812 耗时14.769220352172852s\n",
      "epoch 174 -1.6071332095781283 耗时14.919640302658081s\n",
      "epoch 175 -1.6091137302596135 耗时14.714269161224365s\n",
      "epoch 176 -1.6012303620545052 耗时14.96305799484253s\n",
      "epoch 177 -1.5944092828410668 耗时14.935627460479736s\n",
      "epoch 178 -1.593571680141742 耗时14.838129997253418s\n",
      "epoch 179 -1.5844486479825373 耗时14.777807474136353s\n",
      "epoch 180 -1.5759968327610827 耗时14.619158029556274s\n",
      "epoch 181 -1.5733809543648296 耗时14.947901487350464s\n",
      "epoch 182 -1.5650238453896603 耗时14.958215475082397s\n",
      "epoch 183 -1.5660379731922291 耗时14.72352385520935s\n",
      "epoch 184 -1.5604800931163125 耗时14.843293190002441s\n",
      "epoch 185 -1.5502938622471616 耗时14.729502201080322s\n",
      "epoch 186 -1.5568225698639007 耗时14.716086626052856s\n",
      "epoch 187 -1.548986123745571 耗时14.72489595413208s\n",
      "epoch 188 -1.5419875588208405 耗时14.98237133026123s\n",
      "epoch 189 -1.5447156536159292 耗时15.01216173171997s\n",
      "epoch 190 -1.532368375690159 耗时15.176399230957031s\n",
      "epoch 191 -1.533452895178739 耗时15.006346940994263s\n",
      "epoch 192 -1.5235063006809197 耗时14.867284774780273s\n",
      "epoch 193 -1.5241571157820832 耗时14.721759557723999s\n",
      "epoch 194 -1.5143398007053956 耗时15.146708250045776s\n",
      "epoch 195 -1.4948003769175346 耗时14.625917196273804s\n",
      "epoch 196 -1.510913347103807 耗时14.863141536712646s\n",
      "epoch 197 -1.5007872765321233 耗时14.874223232269287s\n",
      "epoch 198 -1.5028560242378088 耗时14.8567533493042s\n",
      "epoch 199 -1.4985086468774964 耗时14.670335054397583s\n",
      "epoch 200 -1.4952041436793837 耗时14.868601560592651s\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "time0 = time.time()\n",
    "for epoch in range(200):\n",
    "    print(f'epoch {epoch+1}', end=' ')\n",
    "    dev.train()\n",
    "    time0, time_spend = time.time(), time.time()-time0\n",
    "    print(f'耗时{time_spend}s')\n",
    "    if not epoch % 1:\n",
    "        Tools.sample_image(10, epoch+1, dev.g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72b9605e-5c61-433b-9006-6ffbaae6f215",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1da81fb5-0834-4706-b0e5-f9c8436e3d2a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
